package com.project2.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.bo.IssueCreateBO;
import com.project2.bean.po.MarketIssue;
import com.project2.bean.po.MarketIssueExample;
import com.project2.bean.vo.IssueCreateVO;
import com.project2.bean.vo.IssueInfoListVO;
import com.project2.mapper.MarketIssueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService{


    @Autowired
    MarketIssueMapper issueMapper;


    /**
     * 显示  问题
     * @param question
     * @param pageBo
     * @return
     */
    @Override
    public CommonData issueInfo(String question, PageBo pageBo) {


        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        ArrayList<IssueInfoListVO> listVOS = new ArrayList<>();

        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (question == null) {


            //null 可以取出所有数据
            List<MarketIssue> issueList = issueMapper.selectByExample(example);

            //IssueInfoListVO 返回的类中类


            for (MarketIssue Issue : issueList) {
                IssueInfoListVO vO = new IssueInfoListVO();
                vO.setId(Issue.getId());
                vO.setAnswer(Issue.getAnswer());
                vO.setQuestion(Issue.getQuestion());
                listVOS.add(vO);

            }

            PageInfo<IssueInfoListVO> pageInfo = new PageInfo<>(listVOS);

            return CommonData.data(pageInfo);
        }



        criteria.andQuestionLike("%"+question+"%");


        List<MarketIssue> issueList = issueMapper.selectByExample(example);



        for (MarketIssue Issue : issueList) {
            IssueInfoListVO vO = new IssueInfoListVO();
            vO.setId(Issue.getId());
            vO.setAnswer(Issue.getAnswer());
            vO.setQuestion(Issue.getQuestion());
            listVOS.add(vO);

        }

        PageInfo<IssueInfoListVO> pageInfo = new PageInfo<>(listVOS);

        return CommonData.data(pageInfo);


    }


    /**
     * 更新问题
     * @param Issue
     * @return
     */

    @Override
    public MarketIssue issueUpdate(MarketIssue Issue) {


        Integer id = Issue.getId();
        Date date = new Date();
        Issue.setUpdateTime(date);

        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        int IssueUpdate = issueMapper.updateByExample(Issue,example);
        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(id);


        return marketIssue;
    }


    /**
     * 创建问题
     * @param createBO
     * @return
     */
    @Override
    public IssueCreateVO issueCreate(IssueCreateBO createBO) {

        Date date = new Date();

        MarketIssue newIssue = new MarketIssue();
        newIssue.setAnswer(createBO.getAnswer());
        newIssue.setQuestion(createBO.getQuestion());
        newIssue.setUpdateTime(date);
        newIssue.setAddTime(date);

        int issue = issueMapper.insertByAAndQ(newIssue);

        IssueCreateVO issueCreateVO = new IssueCreateVO();
        issueCreateVO.setId(newIssue.getId());
        issueCreateVO.setAnswer(createBO.getAnswer());
        issueCreateVO.setQuestion(createBO.getQuestion());
        issueCreateVO.setUpdateTime(date);

        return issueCreateVO;
    }


    /**
     *删除问题
     * @param i
     * @return
     */
    @Override
    public int issueDelete(IssueCreateVO i) {
        //物理删除
        // int i = issueMapper.deleteByPrimaryKey(issue.getId());

        //逻辑删除
        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(i.getId());

        MarketIssue issue = new MarketIssue();
        issue.setDeleted(true);

        int num = issueMapper.updateByExampleSelective(issue, example);

        return num;
    }
}
